// /common/comFun/spc-utils.js
function calcXR(data) {
  const xPoints = []
  const rPoints = []
  const avgList = []
  const rangeList = []

  data.forEach((item, idx) => {
    const samples = [item.sample1, item.sample2, item.sample3, item.sample4, item.sample5]
    const avg = samples.reduce((a, b) => a + b, 0) / samples.length
    const range = Math.max(...samples) - Math.min(...samples)
    item.mean = avg
    item.range = range
    item.min = Math.min(...samples)
    item.max = Math.max(...samples)
    item.std = Math.sqrt(samples.reduce((sum, val) => sum + Math.pow(val - avg, 2), 0) / samples.length)

    avgList.push(avg)
    rangeList.push(range)
    xPoints.push({ category: idx + 1, value: avg.toFixed(3) })
    rPoints.push({ category: idx + 1, value: range.toFixed(3) })
  })

  const CLx = avgList.reduce((a, b) => a + b, 0) / avgList.length
  const CLr = rangeList.reduce((a, b) => a + b, 0) / rangeList.length
  const A2 = 0.577, D3 = 0, D4 = 2.114
  const UCLx = CLx + A2 * CLr
  const LCLx = CLx - A2 * CLr
  const UCLr = D4 * CLr
  const LCLr = D3 * CLr

  return {
    xChartData: {
      categories: xPoints.map(p => p.category),
      series: [
        { name: '平均值', data: xPoints.map(p => parseFloat(p.value)) }
      ],
      markLines: [
        { value: parseFloat(UCLx.toFixed(3)), color: '#ff0000', label: 'UCL' },
        { value: parseFloat(CLx.toFixed(3)), color: '#0000ff', label: 'CL' },
        { value: parseFloat(LCLx.toFixed(3)), color: '#ff0000', label: 'LCL' }
      ]
    },
    rChartData: {
      categories: rPoints.map(p => p.category),
      series: [
        { name: '极差', data: rPoints.map(p => parseFloat(p.value)) }
      ],
      markLines: [
        { value: parseFloat(UCLr.toFixed(3)), color: '#ff0000', label: 'UCL' },
        { value: parseFloat(CLr.toFixed(3)), color: '#0000ff', label: 'CL' },
        { value: parseFloat(LCLr.toFixed(3)), color: '#ff0000', label: 'LCL' }
      ]
    }
  }
}

function calcSampleTrend(data) {
  return {
    categories: data.map((_, i) => i + 1),
    series: [
      {
        name: '样本平均值',
        data: data.map(item => parseFloat(item.mean.toFixed(3)))
      }
    ]
  }
}

function calcCPK(data) {
  const usl = 1.0
  const lsl = 0.85
  const series = data.map((item, i) => {
    const cpu = (usl - item.mean) / (3 * item.std)
    const cpl = (item.mean - lsl) / (3 * item.std)
    const cpk = Math.min(cpu, cpl)
    return parseFloat(cpk.toFixed(3))
  })
  return {
    categories: data.map((_, i) => i + 1),
    series: [{ name: 'CPK值', data: series }]
  }
}

function calcMeanTrend(data) {
  return {
    categories: data.map((_, i) => i + 1),
    series: [{ name: '均值', data: data.map(d => parseFloat(d.mean.toFixed(3))) }]
  }
}

function calcNormalDistribution(data) {
  const avgAll = data.reduce((sum, d) => sum + d.mean, 0) / data.length
  const stdAll = Math.sqrt(data.reduce((sum, d) => sum + Math.pow(d.mean - avgAll, 2), 0) / data.length)
  const bins = Array(7).fill(0)
  const binEdges = [0.85, 0.88, 0.91, 0.94, 0.97, 1.0, 1.03, 1.06]
  data.forEach(d => {
    const val = d.mean
    for (let i = 0; i < bins.length; i++) {
      if (val >= binEdges[i] && val < binEdges[i + 1]) {
        bins[i]++
        break
      }
    }
  })
  return {
    categories: ['0.85~0.88', '0.88~0.91', '0.91~0.94', '0.94~0.97', '0.97~1.0', '1.0~1.03', '1.03~1.06'],
    series: [{ name: '频数', data: bins }]
  }
}

function calcPassRate(data) {
  const usl = 1.0
  const lsl = 0.85
  const passRates = data.map(item => {
    const samples = [item.sample1, item.sample2, item.sample3, item.sample4, item.sample5]
    const passCount = samples.filter(v => v >= lsl && v <= usl).length
    return parseFloat((passCount / samples.length).toFixed(2))
  })
  return {
    categories: data.map((_, i) => i + 1),
    series: [{ name: '合格率', data: passRates }]
  }
}

export default {
  calcXR,
  calcSampleTrend,
  calcCPK,
  calcMeanTrend,
  calcNormalDistribution,
  calcPassRate
};
